﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Framework;
using Web.Framework.Svc;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.Code
{
    public abstract class BasePage : BaseWebPage
    {
        protected WebServiceHelper WebServiceHelper { get; set; }

        protected UIHelper UIHelper { get; set; }

        protected override void OnPreLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                WebServiceHelper = new WebServiceHelper();
                WebServiceHelper.ServiceOptionsDefault = new ServiceOptions();
                WebServiceHelper.ServiceOptionsDefault.UserName = BaseWebPage.UserName;
                WebServiceHelper.ServiceOptionsDefault.CompanyId = BaseWebPage.CompanyId;
                WebServiceHelper.ServiceOptionsDefault.ApplicationId = BaseWebPage.ApplicationId;

                base.LoadOperations(WebServiceHelper.GetOperationsForUser(BaseWebPage.ApplicationId, BaseWebPage.CompanyId, BaseWebPage.UserName));

                var securityContext = GetSecurityContextForDestinos();
                WebServiceHelper.ServiceOptionsDefault.Operations = securityContext.Operations;
                WebServiceHelper.ServiceOptionsDefault.IsAdmin = securityContext.IsAdmin;

                UIHelper = new UIHelper();
                UIHelper.companyId = BaseWebPage.CompanyId;
                UIHelper.WebServiceHelper = WebServiceHelper;
                
                //masterPage.PreLoad(new List<string>());
            }

            base.OnPreLoad(e);
        }

        public SecurityContextForUser GetSecurityContext()
        {
            SecurityContextForUser securityContextForUser = new SecurityContextForUser();

            securityContextForUser.IsAdmin = BaseWebPage.IsAdmin;
            securityContextForUser.Operations = OperationsForUser;

            return securityContextForUser;
        }

        public SecurityContextForUser GetSecurityContextForDestinos()
        {
            SecurityContextForUser securityContext = new SecurityContextForUser();
            securityContext.IsAdmin = BaseWebPage.IsAdmin;
            List<string> operacionesFinales = new List<string>();
            List<int> entityIds = new List<int>();

            string userName = this.Page.User.Identity.Name;

            operacionesFinales = (List<string>)HttpContext.Current.Session[userName + "destinoOperation"];
            entityIds = (List<int>)HttpContext.Current.Session[userName + "destinoOperationEntity"];
            if (operacionesFinales == null || operacionesFinales.Count == 0)
            {
                WebServiceHelper.ServiceOptionsDefault.IsAdmin = true;
                List<Destino> destinoList = WebServiceHelper.GetDestinos(BaseWebPage.CompanyId, string.Empty);
                WebServiceHelper.ServiceOptionsDefault.IsAdmin = BaseWebPage.IsAdmin;

                List<Destino> operacionesEnDestino = destinoList.Where(d => !String.IsNullOrEmpty(d.SecurityKey)).ToList();
                securityContext.Total = operacionesEnDestino.Count;

                operacionesFinales = new List<string>();
                entityIds = new List<int>();
                // Otengo los destinos y las operaciones de los destinos
                foreach (var dest in operacionesEnDestino)
                {
                    if (securityContext.IsAdmin || OperationsForUser.Contains(dest.SecurityKey))
                    {
                        operacionesFinales.Add(dest.SecurityKey);
                        entityIds.Add(dest.Id);                        
                    }
                }

                HttpContext.Current.Session[userName + "destinoOperation"] = operacionesFinales;
                HttpContext.Current.Session[userName + "destinoOperationEntity"] = entityIds;           
            }

            securityContext.Operations = operacionesFinales;
            securityContext.EntityId = entityIds;

            return securityContext;
        }

        public static void ClearReportingConfiguration(HttpApplicationState application)
        {
            string reportingConfigurationKey = "reportingConfigurationKey";
            application[reportingConfigurationKey] = null;
        }

        public static List<ReportingConfiguration> GetReportingConfiguration(HttpApplicationState application)
        {
            string reportingConfigurationKey = "reportingConfigurationKey";
            List<ReportingConfiguration> reportingConfigurationList;
            if (application[reportingConfigurationKey] == null)
            {
                WebServiceHelper wsHelper = new WebServiceHelper();
                reportingConfigurationList = wsHelper.GetReportingConfiguration(string.Empty);

                application[reportingConfigurationKey] = reportingConfigurationList;
            }
            else
            {
                reportingConfigurationList = (List<ReportingConfiguration>)application[reportingConfigurationKey];
            }

            return reportingConfigurationList;
        }
    }
}